// File: useUpload.ts
import { ref } from "vue";
import Papa from "papaparse";

export const useUpload = () => {
  const tableData = ref([]);

  const uploadRequest = (data: any) => {
    const file = data.file;
    if (!file.name.endsWith(".csv")) {
      alert("请上传CSV文件");
      return;
    }

    // 解析CSV文件内容
    const reader = new FileReader();
    reader.onload = (event) => {
      const csvContent = event.target?.result as string;
      const parsedData = Papa.parse(csvContent, {
        header: true,
        skipEmptyLines: true,
      });

      // 更新表格数据
      tableData.value = parsedData.data.map((row: any) => ({
        Position: row.Position,
        Force: row.Force,
      }));
    };
    reader.readAsText(file);
  };

  return {
    tableData,
    uploadRequest,
  };
};
